<?php

/**
 * This is the model class for table "bank".
 *
 * The followings are the available columns in table 'bank':
 * @property string $id
 * @property string $name
 * @property string $full_name
 * @property string $image
 * @property integer $status
 * @property integer $create_by
 * @property integer $modified_by
 */
class Bank extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Bank the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bank';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, full_name, create_by', 'required'),
			array('status, create_by, modified_by', 'numerical', 'integerOnly'=>true),
			array('name, full_name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, full_name, status, create_by, modified_by', 'safe', 'on'=>'search'),
            array('image', 'file', 
                'types'     => 'gif, jpg, png',
                'maxSize'   =>1024 * 1024 * 1,
                'tooLarge'  =>'The file was larger than 1MB. Please upload a smaller file.',
                'allowEmpty'=>true,
                )
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'full_name' => 'Full Name',
			'image' => 'Image',
			'status' => 'Status',
			'create_by' => 'Create By',
			'modified_by' => 'Modified By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_by',$this->create_by);
		$criteria->compare('modified_by',$this->modified_by);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    public function getImagePath(){
        if($this->image == null)
                return Yii::app()->getRequest()->getBaseUrl(true).'/protected/medias/bank/logo/no-logo.png';
        return Yii::app()->getRequest()->getBaseUrl(true).'/protected/medias/bank/logo/'.$this->image;
    }
    
    public static function findByUser($user_id){
        $userBank = UserBank::model()->findAllByAttributes(array('user_id' => $user_id));
        $list = array();
        foreach ($userBank as $bank){
            $list[$bank->bank_id] = $bank->bank->name; 
        }
        return $list;
    }
}